package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.article.dto.ArticleDto;
import com.heima.article.dto.ArticleHomeDto;
import com.heima.article.dto.ArticleInfoDto;
import com.heima.article.entity.ApArticle;
import com.heima.article.entity.ApArticleContent;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.IApArticleContentService;
import com.heima.article.service.IApArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.service.ITemplateService;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 文章信息表，存储已发布的文章 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-03-14
 */
@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements IApArticleService {
    @Autowired
    private IApArticleContentService contentService;
    @Autowired
    private ITemplateService templateService;
    //保存文章
    @Override
    public ResponseResult<Long> saveArticle(ArticleDto dto) {
        //取出id
        ApArticle article = new ApArticle();
        BeanUtils.copyProperties(dto, article);
        //判断是否存在id
        if (dto.getId() == null) {
            //不存在就新增id
            article.setCreatedTime(new Date());
            this.save(article);
            //新增文章内容
            ApArticleContent content = new ApArticleContent();
            content.setArticleId(article.getId());
            content.setContent(dto.getContent());
            contentService.save(content);
            //生成异步静态HTML 需要异步处理
            System.out.println(Thread.currentThread().getName()+"开始调用成异步静态HTML服务");
            templateService.createHtml(article,dto.getContent());
            System.out.println(Thread.currentThread().getName()+"成异步静态HTML服务调用结束");
            //返回通用响应
            return ResponseResult.okResult(article.getId());
        } else {
            //id不为空,修改文章内容
            this.updateById(article);
            //修改内容表
            LambdaUpdateWrapper<ApArticleContent> update=new LambdaUpdateWrapper<>();
            update.eq(ApArticleContent::getArticleId, dto.getId());
            update.set(ApArticleContent::getContent, dto.getContent());
            contentService.update(update);
            //生成异步静态HTML 需要异步处理
            System.out.println(Thread.currentThread().getName()+"开始调用成异步静态HTML服务");
            templateService.createHtml(article,dto.getContent());
            System.out.println(Thread.currentThread().getName()+"成异步静态HTML服务调用结束");
            return ResponseResult.okResult(article.getId());
        }

    }
//加载文章列表
    @Override
    public ResponseResult loadArticle(ArticleHomeDto dto, int type) {
        //需求分析
        //默认加载10片文章
        if (dto.getSize()<=0||dto.getSize()>50) {
            dto.setSize(20);
        }
        LambdaQueryWrapper<ApArticle> query = new LambdaQueryWrapper<>();
        //根据频道id查询
        if (dto.getChannelId()!=null&&dto.getChannelId()!=0) {
            query.eq(ApArticle::getChannelId, dto.getChannelId());
        }
        //文章按照发布时间倒排
        query.orderByDesc(ApArticle::getPublishTime);
        if (type == 1) {
            //上拉获取更多文章需要判断文章的发布时间小于当前传入最小时间
            query.lt(ApArticle::getPublishTime, dto.getMinTime());
        } else {
            //下拉获取最新文章,需要判断文章发布时间大于当前传入的最大时间
            query.gt(ApArticle::getPublishTime, dto.getMaxTime());
        }
        //判断文章是否删除或下架
        query.eq(ApArticle::getIsDelete, false);
        query.eq(ApArticle::getIsDown, false);
        //分页查询
        IPage<ApArticle> page = new Page<>(1, dto.getSize());
        IPage<ApArticle> iPage = this.page(page, query);
        return ResponseResult.okResult(iPage.getRecords());
    }

    //加载文章详情
    @Override
    public ResponseResult loadArticleInfo(ArticleInfoDto dto) {
        //根据id查询文章
        ApArticle apArticle = this.getById(dto.getArticleId());
        if (apArticle==null||apArticle.getIsDelete()||apArticle.getIsDown()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //根据id查询文章内容
        LambdaQueryWrapper<ApArticleContent> query=new LambdaQueryWrapper<>();
        query.eq(ApArticleContent::getArticleId, dto.getArticleId());
        ApArticleContent apArticleContent = contentService.getOne(query);
        //按照前端格式拼接map
        Map<String, Object> map = new HashMap<>();
        map.put("config", apArticle);
        map.put("content", apArticleContent);

        //返回通用响应
        return ResponseResult.okResult(map);
    }

}
